package lk.mrt.cse.wihidum.router.receiver;

import lk.mrt.cse.wihidum.router.processor.*;
import org.apache.log4j.Logger;

import java.util.ArrayList;

public class Service
{
    private static final Logger logger = Logger.getLogger (Service.class);
    AdminProcessor adminProcessor;

    public Service()
    {
         adminProcessor = new AdminProcessor();
    }
    public String getMessageTrace(String adminAddress)
    {
        AdminMessageTraceProcessor processor = new AdminMessageTraceProcessor();
        return processor.process(adminAddress);
    }

    public String publish(String brokerAddress,  String adminAddress, String publisherAddress, String topic, String publicationText)
    {
        PublicationProcessor processor = new PublicationProcessor();
        return processor.publish(brokerAddress, adminAddress, publisherAddress, topic, publicationText);
    }

    public String subscribe(String subscriptionID, String brokerAddress, String adminAddress, String listenerAddress, String subscriberAddress, String topic) {
        SubscriptionProcessor processor = new SubscriptionProcessor();
        return processor.subscribe(subscriptionID, brokerAddress, adminAddress, listenerAddress, subscriberAddress, topic);
    }

    public String getPublications(String subscriptionID, String subscriberAddress, String listenerAddress) {
        GetPublicationsProcessor processor = new GetPublicationsProcessor();
        return processor.getPublications(subscriptionID, subscriberAddress, listenerAddress);
    }

    public String unsusbcribe(String listenerServiceAddress, String clientID, String filterTopic) {
        logger.info("Router service received an unfollow request");
        UnsubscribeProcessor processor =  new UnsubscribeProcessor();
        return processor.unsusbcribe(listenerServiceAddress, clientID, filterTopic);
    }

    public String insertBroker(String adminAddress, String brokerAddress)
    {
       return adminProcessor.insertBroker(adminAddress, brokerAddress);
    }

    public String changeTopology (String adminAddress, String topology)
    {
       return adminProcessor.changeTopology(adminAddress, topology);
    }

    public String registerClient(String name, String password, String age, String country)
    {
       return adminProcessor.registerClient(name, password, age, country);
    }

    public String getClientNames()
    {
       return adminProcessor.getClients(); 
    }

    public ArrayList<String> getSubscriptions(String listenerAddress, String subscriberID)
    {
        ArrayList<String> subscribedTopics=null;
        SubscriptionProcessor processor=new SubscriptionProcessor();
        subscribedTopics =  processor.getsubscribedTopics(listenerAddress, subscriberID);
        return subscribedTopics;
    }

    public String getFriends(String clientID, String listenerAddress)
    {
        ArrayList<String> subscribedTopics= this.getSubscriptions(listenerAddress, clientID);
        String result = "";
        if(subscribedTopics!= null)
            result = adminProcessor.getFriends(subscribedTopics);
        return result;
    }
}
